package com.hp.attendance.service;

import com.hp.attendance.dao.HistoryBysDao;
import com.hp.attendance.dao.HistoryDao;
import com.hp.attendance.entity.*;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class HistoryBysService {
    private HistoryBysDao historyBysDao = new HistoryBysDao();
    private HistoryDao historyDao = new HistoryDao();

    /**
     * 查询员工档案（包含离休和在职的所有员工信息）
     * @return
     */
    public List<History> getHisList() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        List<History> list = historyBysDao.getHisList();
        return list;
    }

    /**
     * 根据部门编号查询部门名称
     * @param department_number
     * @return
     */
    public Department getDepName(int department_number) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Department department = historyBysDao.getDepName(department_number);
        return department;
    }

    /**
     * 根据职位编号查询职位名称
     * @param position_number
     * @return
     */
    public Position getPosName(int position_number) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Position position = historyBysDao.getPosName(position_number);
        return position;
    }

//------------------------------------------分页功能⬇------------------------------------------------
    /**
     * 分页查询记录
     * @param pageNo 当前页
     * @param pageSize 每页显示的条数
     * @return
     */
    public List<History> getLimitList(int pageNo, int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {

        return historyBysDao.getLimitList(pageNo,pageSize);
    }

    /**
     * 查询总记录数
     * @return
     * @throws SQLException
     */
    public long getTotal() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {

        return historyBysDao.getTotal();
    }


    /**
     * 分页查询员工档案列表
     * @param pageNo 当前页
     * @param pageSize 每页显示的条数
     * @return
     */
    public PageInfo<HistoryBys> getPage(int pageNo , int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        PageInfo<HistoryBys> page = new PageInfo<HistoryBys>();
        //查询分页的数据                           2       10
        List<History> list = historyBysDao.getLimitList(pageNo, pageSize);

        ArrayList<HistoryBys> historyBys = new ArrayList<HistoryBys>();

        for (History history : list) {
            Integer employee_number = history.getEmployee_number();
            HistoryBys his = new HistoryBys(history.getId(),history.getEmployee_number(),history.getName(),history.getGender(),
                    history.getBirthday(),history.getTelephone(),history.getEmail(),history.getAddress(),history.getEducation(),
                    history.getIn_time(),history.getOut_time(),
                    getDepName(history.getDepartment_number()).getName(),
                    getPosName(history.getPosition_number()).getName(),
                    history.getStatus(),history.getHome(),history.getNotes());
            historyBys.add(his);
        }

        page.setRecords(historyBys);
        //查询总记录条数
        long total = historyBysDao.getTotal();
        page.setTotal(total);
        //设置当前页码
        page.setCurrent(pageNo);
        //设置总页数
        int pages = (int)Math.ceil(total / (pageSize * 1.0));
        page.setPages(pages);
        return page;
    }

//------------------------------------------分页功能⬆------------------------------------------------

//---------------------在在职员工管理页面添加员工的同时需要在档案中添加员工数据----------------------
    /**
     * 添加员工信息
     * @param history
     */
    public void AddHistory(History history) throws SQLException, IOException, ClassNotFoundException {
        historyBysDao.AddHistory(history);
    }


//------------------------------从history表中查询状态为离职的员工信息------------------------------
    /**
     * 根据工号查询history信息
     * @param employee_number
     * @return
     */
    public History getHistoryByNum(int employee_number) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        return historyBysDao.getHistoryByNum(employee_number);
    }

//--------------------------------离职员工分页--------------------------------
    /**
     * 分页查询记录
     * @param pageNo 当前页
     * @param pageSize 每页显示的条数
     * @return
     */
    public List<History> getLimitListByStatus(int pageNo, int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        return historyBysDao.getLimitListByStatus(pageNo, pageSize);
    }


    /**
     * 分页查询员工档案列表
     * @param pageNo 当前页
     * @param pageSize 每页显示的条数
     * @return
     */
    public PageInfo<HistoryBys> getPageOut(int pageNo , int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        PageInfo<HistoryBys> page = new PageInfo<HistoryBys>();
        //查询分页的数据                           2       10
        List<History> list = historyBysDao.getLimitListByStatus(pageNo, pageSize);

        ArrayList<HistoryBys> historyBys = new ArrayList<HistoryBys>();

        for (History history : list) {
            Integer employee_number = history.getEmployee_number();
            HistoryBys his = new HistoryBys(history.getId(),history.getEmployee_number(),history.getName(),history.getGender(),
                    history.getBirthday(),history.getTelephone(),history.getEmail(),history.getAddress(),history.getEducation(),
                    history.getIn_time(),history.getOut_time(),
                    getDepName(history.getDepartment_number()).getName(),
                    getPosName(history.getPosition_number()).getName(),
                    history.getStatus(),history.getHome(),history.getNotes());
            historyBys.add(his);
        }

        page.setRecords(historyBys);
        //查询总记录条数
        long total = historyBysDao.getTotalee();

        page.setTotal(total);
        //设置当前页码
        page.setCurrent(pageNo);
        //设置总页数
        int pages = (int)Math.ceil(total / (pageSize * 1.0));
        page.setPages(pages);
        return page;
    }

    //------------------------------------修改离职员工信息--------------------------------------------
    /**
     * 修改员工信息
     * @param history
     */
    public void UpdateHistoryByNum(History history) throws SQLException, IOException, ClassNotFoundException {
        historyBysDao.UpdateHistoryByNum(history);
    }

}
